package de.rj.urwalking.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import de.rj.urwalking.R;
import de.rj.urwalking.config.Config;
import de.rj.urwalking.config.Constants;

/**
 * Contains convenience methods for reading/writing shared preferences.
 * @author rjj
 *
 */

public class PrefUtil {

	/**
	 * Convenience method to read a boolean value from the application's shared preferences.
	 * 
	 * @param key
	 *            The string that acts as key for the preference.
	 * @param context
	 * @param defaultValue
	 * @return The value of the preference, if it exists. Otherwise the supplied defaultValue.
	 */
	public static boolean readBoolean(String key, Context context, boolean defaultValue) {
		return (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(key, defaultValue));
	}

	/**
	 * Convenience method to read a boolean value from the application's shared preferences.
	 * 
	 * @param resId
	 *            The resource id that points to the string resource which acts as key.
	 * @param context
	 * @param defaultValue
	 * @return The value of the preference, if it exists. Otherwise the supplied defaultValue.
	 */
	public static boolean readBoolean(int resId, Context context, boolean defaultValue) {
		return (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(context.getString(resId),
				defaultValue));
	}

	public static int readInt(String key, Context context, int defValue) {
		return (PreferenceManager.getDefaultSharedPreferences(context).getInt(key, defValue));
	}

	public static String readString(String key, Context context, String defValue) {
		return (PreferenceManager.getDefaultSharedPreferences(context).getString(key, defValue));
	}

	public static String readString(int resId, Context context, String defValue) {
		return (PreferenceManager.getDefaultSharedPreferences(context).getString(context.getString(resId), defValue));
	}

	public static String readPoiVisibility(Context context) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		return prefs.getString(context.getString(R.string.key_pref_show_pois),
				context.getString(R.string.pref_show_pois_default));
	}

	/**
	 * Convenience method to access the radius preference.
	 * 
	 * @param context
	 * @return the loading radius, as specified by the user in the preferences
	 */
	public static double readRadius(Context context) {

		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		String radiusString = prefs.getString(context.getString(R.string.key_pref_radius),
				context.getString(R.string.pref_radius_default));

		return Double.valueOf(radiusString);
	}

	/**
	 * Convenience method to access the maximum radius preference.
	 * 
	 * @param context
	 * @return the maximum radius, as specified by the user in the preferences
	 */
	public static double readMaxRadius(Context context) {

		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		String radiusString = prefs.getString(context.getString(R.string.key_pref_max_radius),
				context.getString(R.string.pref_max_radius_default));

		return Double.valueOf(radiusString);
	}

	public static float readFov(Context context) {

		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		String fovString = prefs.getString(context.getString(R.string.key_pref_fov),
				context.getString(R.string.pref_fov_default));

		return Float.valueOf(fovString);

	}

	public static int readParticipantId(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context).getInt("participantId",
				Config.TEST_PARTICIPANT_ID);

	}

	public static void saveBoolean(String key, boolean value, Context context) {

		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	public static void saveInt(String key, int value, Context context) {

		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putInt(key, value);
		editor.commit();
	}
	
	public static boolean inTestMode(Context context) {
		return readBoolean(R.string.key_pref_test_mode, context, false);
	}
	
	public static void clearCredentials(Context context) {

		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putInt("participantId", Constants.UNDEFINED);
		editor.putString("email", Constants.EMPTY_STRING);
		editor.putBoolean("loggedIn", false);
		editor.commit();

	}
	

}
